package TwoPoint;

/**
 * @description: 描述
 * @author: Mr.Hu
 * @create: 2022/3/20 11:10
 **/
public class OrderSquareSort {
    public static void main(String[] args) {
        int [] nums = {-2,0};
        OrderSquareSort orderSquareSort = new OrderSquareSort();
        int[] ints = orderSquareSort.sortedSquares(nums);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    public int[] sortedSquares(int[] nums) {
        int fenGe=-1;
        for (int i = 0; i <= nums.length-1; i++) {
            if (nums[i]<0){
                fenGe = i;
            }else {
                break;
            }
        }
        int[] ans = new int[nums.length];
        int index = 0,i = fenGe, j = fenGe+1;
        while ( i >=0 || j<nums.length) {
            if(i<0){
                ans[index] = nums[j]*nums[j];
                j++;
            }else if(j>nums.length-1){
                ans[index] = nums[i]*nums[i];
                i--;
            }else if(nums[i]*nums[i]<=nums[j]*nums[j]){
                ans[index] = nums[i]*nums[i];
                i--;
            }
            else{
                ans[index] = nums[j]*nums[j];
                j++;
            }
            index++;
        }
        return ans;
    }
}
